﻿package com.moonshineproject.plugin.actionscript.as3project.vo 
{
	import __AS3__.vec.Vector;
	
	import com.moonshineproject.plugin.project.vo.FileWrapper;
	
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	public class MXMLCConfigVO extends FileWrapper 
	{
		public function MXMLCConfigVO(file:File=null)
		{
			super(file);
		}
		
		public function write(pvo:AS3ProjectVO):void 
		{
			if (pvo.targets.length == 0) 
			{
				// No targets found for config construction.
				return;
			}
			
			var i:int;
			
			var oldIC:Boolean = XML.ignoreComments;
			XML.ignoreComments = false;
			
			var data:XML = <flex-config />;
				
			XML.ignoreComments = oldIC;
			
			data.appendChild(
				<target-player>{pvo.swfOutput.swfVersion}.0.0</target-player>
			);

			data.appendChild(
				<compiler />
			);
			
			// TODO add locales
			
			// TODO built-in compiler constants like CONFIG::debug, CONFIG::release and CONFIG::timeStamp
			var compilerConstants:Vector.<String> = Vector.<String>([]);
			if (pvo.buildOptions.compilerConstants && pvo.buildOptions.compilerConstants != "")
				compilerConstants = Vector.<String>(pvo.buildOptions.compilerConstants.split("\n"));
				
			for each (var constant:String in compilerConstants) 
			{
				var constantSp:Array = constant.split(",");
				data.compiler.appendChild(<define><name>{constantSp[0]}</name><value>{constantSp[1]}</value></define>);
			}
			
			if (pvo.classpaths.length > 0)			data.compiler.appendChild(exportPaths(pvo.classpaths, <source-path append="true"/>, <path-element/>));
			if (pvo.libraries.length > 0)			data.compiler.appendChild(exportPaths(pvo.libraries, <library-path append="true"/>, <path-element/>));
			// TODO possibly have to iterate through all the swc files in the dir and add their paths manually?
			if (pvo.includeLibraries.length > 0)	data.compiler.appendChild(exportPaths(pvo.includeLibraries, <include-libraries append="true"/>, <library/>));
			if (pvo.externalLibraries.length > 0)	data.compiler.appendChild(exportPaths(pvo.externalLibraries, <external-library-path append="true"/>, <path-element/>));
			// TODO pvo.runtimeSharedLibraries
			
			data.appendChild(exportPaths(pvo.targets, <file-specs/>, <path-element/>));
			
			// SWF
			data.appendChild(<default-background-color>#{pvo.swfOutput.backgroundColorHex}</default-background-color>);
			data.appendChild(<default-frame-rate>{pvo.swfOutput.frameRate}</default-frame-rate>);
			data.appendChild(<default-size><width>{pvo.swfOutput.width}</width><height>{pvo.swfOutput.height}</height></default-size>);
			
			var dataStr:String = 	
				"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"+
				"<!--This Adobe Flex compiler configuration file was generated by a tool.-->\n"+
				"<!--Any modifications you make may be lost.-->\n"+
				data.toXMLString();
			
			if (!file)
			{
				var dir:File = pvo.folder.resolvePath("obj/");
				if (!dir.exists) dir.createDirectory();
				
				file = dir.resolvePath(pvo.projectName+"Config.xml");
			}
			
			
			// Write file
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(dataStr);
			stream.close();
		}
		
		private function exportPaths(v:Vector.<File>, container:XML, element:XML):XML {
			for each (var f:File in v) {
				var e:XML = element.copy();
				e.appendChild(f.nativePath);
				container.appendChild(e);
			}
			return container;
		}
	}
}